/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *                                                                           *
 *                          Copyright (c) 2010                               *
 *                  Alen Stojanov [ ioi.alen@gmail.com ]                     *
 *                Jacobs University Bremen, Bremen, Germany                  *
 *                                                                           *
 *                          All Rights Reserved.                             *
 *                                                                           *
 *    Permission to use, copy, modify and distribute this software and       *
 *    its documentation is hereby granted, provided that both the            *
 *    copyright notice and this permission notice appear in all copies of    *
 *    the software, derivative works or modified versions, and any           *
 *    portions thereof, and that both notices appear in supporting           *
 *    documentation.                                                         *
 *                                                                           *
 *    This code is distributed in the hope that it will be useful, but       *
 *    WITHOUT ANY WARRANTY; without even the implied warranty of             *
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS       *
 *    DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER          *
 *    RESULTING FROM THE USE OF THIS SOFTWARE.                               *
 *                                                                           *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/**
 * Use the following file to convert the data from CinC Challenge 2000 data sets
 * into simple ASCII file format that will be able to be processed by the
 * TSRenderer binary
 *
 * Datasets available at:
 * 		http://www.physionet.org/physiobank/database/apnea-ecg/
 */

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

void cnc2tsdata(string input_file, string output_file, int stop)
{
	ifstream fin  (input_file.c_str(), ios::in|ios::binary);
	ofstream fout (output_file.c_str(), ios::out);

	char c[2];
	long x = 0;
	unsigned int y = 0;

	if ( fin.is_open() ) {
		while ( !fin.eof() ) {
			fin.read(c, sizeof(c));
			y =            (unsigned char)c[0];
			y = (y << 8) | (unsigned char)c[1];
			cout << x << " " << y << endl;
			fout << x++ << " " << y << endl;
			if ( stop != -1 && x > stop ) {
				break;
			}
		}
	}
	fin.close();
	fout.close();
}

void usage ()
{

}

int main (int argc, char* argv[])
{
	int stop = -1;

	if ( argc < 3 ) {
		usage();
		return 0;
	}

	string input(argv[1]);
	string output(argv[2]);

	if (argc > 3) {
		stop = atoi(argv[3]);
	}

	cnc2tsdata(input, output, stop);

	return 0;
}
